//
//  HWRegisterAndLoginManager.m
//  嗨文
//
//  Created by 响当当 on 2019/3/20.
//  Copyright © 2019年 dongzheng. All rights reserved.
//

#import "HWRegisterAndLoginManager.h"

@implementation HWRegisterAndLoginManager
+ (HWRegisterAndLoginManager *)manager
{
    static HWRegisterAndLoginManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (!manager) {
            manager = [[HWRegisterAndLoginManager alloc]init];
        }
    });
    return manager;
}
/**
 *   注册账号
 */
- (void)registUser:(NSDictionary *)prarmers sucess:(void(^_Nullable)(id _Nonnull acceptData))sucess fail:(nonnull void(^)( NSError * _Nullable error))fail
{
    [NetWorkingManager postRequestUrl:FULL_WEB_URL(KAPI_REGIST) withPramater:prarmers sucess:^(id responseObject) {
        sucess(responseObject);
    } failure:^(NSError *error) {
        fail(error);
    }];
}
/**
 *   获取验证码
 */
- (void)getCodeWithPhone:(NSString *)phoneStr Sucess:(void(^)(id acceptData))sucess fail:(nonnull void(^)( NSError * _Nullable error))fail
{
    
    NSDictionary *prarmers = @{
                               @"phoneNumber" : phoneStr
                               };
    [NetWorkingManager postRequestUrl:FULL_WEB_URL(kAPI_getCode) withPramater:prarmers sucess:^(id responseObject) {
        sucess(responseObject);
    } failure:^(NSError *error) {
        fail(error);
    }];
    
}

- (void)getLoginRequestWithParamers:(NSDictionary *)paramers sucess:(void (^)(void))sucess fail:(void (^)(NSError *error))fail
{
    [NetWorkingManager postRequestUrl:FULL_WEB_URL(KAPI_LOGIN) withPramater:paramers sucess:^(id responseObject) {
        
        NSDictionary *dic = responseObject[@"data"];
        
        NSString *token = dic[@"sessionId"];
        current_User.token = token;
        current_User.username = dic[@"phoneNum"];
        current_User.userID = dic[@"userId"];
        current_User.nickName = dic[@"nickName"];
        current_User.isHideinfo = dic[@"isAppInfo"];
        current_User.hasLogin = @"1";
        
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            [current_User savaSessionSucess:^{
                if (sucess) {
                    sucess();
                }
            } fail:^(NSError *error) {
                
            }];
        }];
        
        
    } failure:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"登录失败，请重试"];
        DISMISS_SVP(1.0f);
        fail(error);
    }];
}

/**
 *  验证token
 */
- (void)checkTokenParamers:(NSDictionary *_Nonnull)prarmers sucess:(void(^_Nullable)(id _Nonnull acceptData))sucess fail:(nonnull void(^)( NSError * _Nullable error))fail
{
    [NetWorkingManager postRequestUrl:FULL_WEB_URL(KAPI_CHECKTOKEN) withPramater:prarmers sucess:^(id responseObject) {
        
        NSDictionary *dic =  responseObject[@"data"];
        if ([dic isValidDict])
        {
            sucess(@200);
        }
        else
        {
            sucess(responseObject[@"code"]);
        }
    } failure:^(NSError *error) {
        fail(error);
    }];
}

@end
